<?
include('../controller/handler.php');

include('../module/var.inc.php');

session_start();

$hd = new Handler();
$base_url = $hd->getBaseURL();
$files = $hd->getUploadedFiles();
if($_GET!=NULL && $hd->getPassword()==sha1($_SESSION['password'])) {
	$id = $_GET['id'];
	if($id=="all") {
		foreach($files as $key => $value) {
			$src = "../uploads/".basename($value);
			unlink($src);
		}
		$hd->writeLog("[FILE] ".strftime("%c")." all files deleted by ".$hd->getUsername()." ID=".
			$_SERVER['REMOTE_ADDR']);
		$feedback = "Tutti i file sono stati eliminati con successo.";
	} else {
		$src = "../uploads/".basename($files[$id]);
		if(unlink($src)){
			$feedback = "File <span class=\"italic\">".basename($files[$id])."</span> eliminato con successo.";
			$hd->writeLog("[FILE] ".strftime("%c")." file ".basename($files[$id])." deleted by ".$hd->getUsername()." ID=".
				$_SERVER['REMOTE_ADDR']);
		} else {
			$feedback = "Errore durante l'eliminazione del file <span class=\"italic\">".$src."</span>.";
		}
	}
	$files = $hd->getUploadedFiles();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="it" lang="it">
<head>
	<meta name="robots" content="noindex, nofollow"/>
	<? include('../module/head.inc.php'); ?>
	<script src="<?echo $base_url;?>js/editor.js" type="text/javascript" charset="utf-8"></script>
</head>

<body>
<div id="content">
<?include('../module/header.inc.php');?>

<?if($hd->getPassword()==sha1($_SESSION['password'])) {?>

<div id="panel_form" class="form">
	<div class="upper_bar_main"><div class="upper_bar_round"></div></div>
	<div class="body_main_left">
	<div class="body_main_right">
	<div class="body_main">
	<?if(count($files)>0) {?>
	<h2>Elimina file</h2>
	<p>Premi con il tasto sinistro del mouse sul file che vuoi eliminare.</p>
	<table id="image_container" border="0" cellspacing="5" cellpadding="5">
		<tr>
		<?$size = 0;
		//$info = " onmouseover=\"showDelete(this)\" onmouseout=\"hideDelete()\"";
		$info = "";
		foreach($files as $key => $value) {?>
			<?$src = $base_url."uploads/".basename($value);
			$temp_size = filesize("../uploads/".basename($value));
			$ratio = ($temp_size/1024>1024)? 1024*1024 : 1024;
			$unit = ($temp_size/1024>1024)? "MB" : "KB";
			$curr = floor($temp_size/$ratio*10)/10;
			$size += $temp_size;?>
			<td>
			<p class="small"><?echo $curr;?> <?echo $unit;?><br /><span class="italic"><?echo basename($value);?></span></p>
			<?if($hd->isImage($value)) {?>
				<a<?echo $info;?> href="<?echo $base_url;?>panel/file/delete:<?echo $key;?>"><img src="<?echo $src;?>" alt="img_<?echo $key;?>" /></a>
			<?} else {?>
				<p class="file"><a<?echo $info;?> href="<?echo $base_url;?>panel/file/delete:<?echo $key;?>"><?echo basename($value);?></a></p>
			<?}?>
			</td>
			<?if(($key+1)%4==0) {?></tr><tr><?}?>
		<?}?>
		</tr>
	</table>
	<?} else {?>
	<h2>Nessun file presente</h2>
	<?}?>
	<?if($feedback) {?><p id="feedback" class="highline"><?echo $feedback;?></p> <?}?>
	<p class="small admin_action">
		<a href="<?echo $base_url;?>panel">&larr; Pannello di amministrazione</a> |
		<a href="<?echo $base_url;?>panel/file">Carica nuovo file</a> |
		<a href="<?echo $base_url;?>panel/file/delete:all">Elimina tutti i file</a> |
		<?echo floor($size/1024/1024*10)/10;?> MB occupati
	</p>
	</div></div></div>
	<div class="lower_bar_main"><div class="lower_bar_round"></div></div>
</div>

<?}?>

<? include('../module/footer.inc.php'); ?>
</div>
</body>
</html>
